﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using GameWatch.Utils;
using GameWatch.Utils.Net;
using System.IO;
using MadeInPortugal.Class;

namespace MadeInPortugalWebApplication
{

    public partial class email : System.Web.UI.Page
    {
        int UserId = 0;
        String UserType;
        IPToCountry Ip2Country = new IPToCountry();
        protected override void OnInit(EventArgs e)
        {
            if (!IsPostBack)
            {

                CheckUser();
                string sApplicationPath = Server.MapPath("~");

                LoadIpCountryTable(sApplicationPath + @"\IP2CountryResource\apnic.latest");
                LoadIpCountryTable(sApplicationPath + @"\IP2CountryResource\arin.latest");
                LoadIpCountryTable(sApplicationPath + @"\IP2CountryResource\lacnic.latest");
                LoadIpCountryTable(sApplicationPath + @"\IP2CountryResource\ripencc.latest");

                #region Finding Visitors IP Address
                string VisitorsIPAddr = string.Empty;
                //Users IP Address.                
                if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
                {
                    //To get the IP address of the machine and not the proxy
                    VisitorsIPAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
                }
                else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
                {
                    VisitorsIPAddr = HttpContext.Current.Request.UserHostAddress;
                }
                #endregion

                string VisitorsCountry = Ip2Country.GetCountry(VisitorsIPAddr);
                Session["visitorIP"] = VisitorsIPAddr;
                Session["visitorCountry"] = VisitorsCountry;
                Session["imgCountryFlag"] = @"~/Flags/" + VisitorsCountry + ".gif";
            }
        }

        private void CheckUser()
        {
            if (Session["UserType"] != null)
            {
                UserType = Session["UserType"].ToString();
                int.TryParse(Session["UserId"].ToString(), out UserId);

                switch (Session["UserType"].ToString())
                {
                    case "Admin":
                        Response.Redirect("AdminProductList.aspx");
                        break;
                    case "Enterprise":
                        navigationTab.Visible = true;
                        myProducts.Visible = true;
                        break;
                    case "Normal":
                        navigationTab.Visible = true;
                        myProducts.Visible = false;
                        break;
                    default:
                        navigationTab.Visible = false;
                        myProducts.Visible = false;
                        break;
                }

            }
            else
            {
                navigationTab.Visible = false;
                myProducts.Visible = false;
            }
        }
        public void LoadIpCountryTable(string name)
        {
            FileStream s = new FileStream(name, FileMode.Open);
            StreamReader sr = new StreamReader(s);
            Ip2Country.Load(sr);
            sr.Close();
            s.Close();
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (Session["Product"] != null)
            {
                Product prod = Session["Product"] as Product;
                liTitle.Text = "Contact -" + prod.ProductName + " - " + prod.Enterprise.EnterpriseName;
            }
        }
    }
}
